
package com.gitee.jmash.websocket.client;

import com.gitee.jmash.common.grpc.client.GrpcChannel;
import io.grpc.ManagedChannel;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;
import jmash.websocket.WebsocketGrpc;

/** websocket Client Config . */
public class WebsocketClientConfig {

  protected static ManagedChannel channel = null;

  public static synchronized ManagedChannel getManagedChannel() {
    if (null != channel && !channel.isShutdown() && !channel.isTerminated()) {
      return channel;
    }
    // k8s环境获取后端服务,本地环境获取测试服务.
    channel = GrpcChannel.getServiceChannel("jmash-websocket-service.jmash.svc.cluster.local");
    return channel;
  }

  /** WebsocketBlockingStub. */
  @Produces
  @Singleton
  public static WebsocketGrpc.WebsocketBlockingStub getWebsocketBlockingStub() {
    return WebsocketGrpc.newBlockingStub(getManagedChannel());
  }

  /** WebsocketFutureStub. */
  @Produces
  @Singleton
  public static WebsocketGrpc.WebsocketFutureStub getWebsocketFutureStub() {
    return WebsocketGrpc.newFutureStub(getManagedChannel());
  }

  /** WebsocketStub. */
  @Produces
  @Singleton
  public static WebsocketGrpc.WebsocketStub getWebsocketStub() {
    return WebsocketGrpc.newStub(getManagedChannel());
  }
}
